
/*======================================================================================================\
|                                          空调配送主列表页面                                                 |
|-------------------------------------------------------------------------------------------------------|
|作者：郭建文                                                                                           |
|时间：2010-09-08 创建并注释                                                                            |
|                                                                                                       |
|说明：◆ 演示最简单基本的列表页面。                                                                    |
|      页面由单个的查询条件（form）+单个的静态表格（grid）构成                                          |
|      ◆ 演示了 查询条件中，出现了多级下拉列表联动的处理过程                                           |
|      ◆ 演示了 如何获得用户当前设置的查询条件，并发出请求，查询数据                                   |
|                                                                                                       |
|-------------------------------------------------------------------------------------------------------|
|                                                                                                       |
|                 必须严格执行代码层次缩进规范！4空格缩进！严禁tab键缩进！                              |
|                                                                                                       |
|                                       Copyright (c) 2009 YuJunJie                                     |
\======================================================================================================*/
var loginUserId;
var loginUserName;

var page1 = new Sail.page({
    name     : "mainList",                  //定义页面对象的唯一标识 名称
    mode     : 'list',                      //这只页面对象的运行模式：列表页面
	isIframe : true,
    
    frames   : {
        name     : "listFrame",                 //该frame对象的唯一标识
        title    : "数据列表",             //标题文本

        //-------------------------------------------------------------------------------
        //以JSON形式描述 frame 的构成(包含哪些 item)
        //-------------------------------------------------------------------------------
        items    : [
             {
                name  : "search",   
                type  : "form", 
                title : "查询条件",
                "isSearch" : true,
                
                "items" : [
	                {
			            type:"calendar", title:"单据日期从", dataIndex:'beginBillDate'    
			        },{
			        	type:"calendar", title:"至", dataIndex:'endBillDate'     
			        },{
			        	type:"input", title:"空调名称",   dataIndex:'materielName'
			        },{
		                  type:"combox", 
		                  title:"门店",
		                  dataIndex : "supplierId",
		                  nameIndex : "supplierName",    
							        allowBlank:true,
							        root:"dataset.item",
							        fieldText:"name",
							        fieldValue:"id",
							        url:"../../../DataDict?type=repairsSupplier"
		                }
			        ,{
						type:"combox", title:"录入人",
						dataIndex : "creatorId",nameIndex : "creatorName",    
						allowBlank:true,root:"dataset.item",fieldText:"name",fieldValue:"id",
						url:"../../../DataDict?type=matBillCreator&cate=params&billType=" + BILL_TYPE_REPAIRS_OUTWAREHOUSE + "&businessType=" + OUTWAREHOUSE_TYPE_OF_LINGYONG
	                }
		        ],
                "layoutConfig" : {
                    columns : 5
                },
                "layout" : [
                    {
                	  items:[
			                {'name':'单据日期从'},
			          	    {'name':'至'},
			          	    {'name':'门店'},
			          	    {'name':'空调名称'},
			          	    {'name':'录入人'}
                           ]
                    }
                ]
            },
            {
                name      : "gridMain",
                type      : "grid",
                title     : "空调配送单列表",
                tbar      : ['新增','-','删除','复制','-'],//'-','审批','-','取消审批','-','确认','-','取消确认','-'],
                pageSize:12,
            	singleSelect:false,
            	height:MAIN_HEIGHT,
                bindRelation   : {
                    root  :"dataset.billVo",
                    id    : "id",
                    field :["transactionId","creatorId","billStatus"],
                    col   :[
                        {"header":"单据日期",  "width":100,   "dataIndex":"billDate","renderer":Sail.formatDate},
                        {"header":"单据编号", "width":100,   "dataIndex":"billCode"},
                        {"header":"门店", "align":"left","width":150,   "dataIndex":"otherDic2Name" },
                        {"header":"资金形式", "align":"left","width":150,   "dataIndex":"otherDic1Name" },
                        //{"header":"审批人", "align":"left","width":75,   "dataIndex":"auditUserName" },
                        //{"header":"确认人", "align":"left","width":75,   "dataIndex":"affirmUserName" },
                        {"header":"录入人", "align":"left","width":75,   "dataIndex":"creatorName" },
                        {"header":"录入时间",   "width":120,   "dataIndex":"createTime","renderer":Sail.toFullDate},
                        {"header":"修改",      "width":50,   "actionCol" :true , renderer:
                        	function(value,p,r){
                 			  var creatorId = r.get('creatorId');
                 			  var billStatus = r.get('billStatus');
                 				return "<span class=actionSpan>修改</span>";
      				      }  
                        }
                    ]
                    
                }
            },
            {
                name      : "gridSub",
                type      : "grid",
                title     : "空调配送单明细",
                "colCheckbox"  : "false",
                bbar:false,
                haveCheckCol:false,
                height:SUB_HEIGHT,
                bindRelation   : {
                    root  :"dataset.billDetailVo",
                    col   :[
                        {"header":"供给形式",align:"left",  "width":90,   "dataIndex":"otherDic1Name" },
                        {"header":"名称规格",align:"left",  "width":200,   "dataIndex":"materielName" },
                        //{"header":"图号",align:"left",  "width":80,   "dataIndex":"figureNO" },
                        //{"header":"货位",align:"left",  "width":80,   "dataIndex":"allocation" },
                        {"header":"单位",align:"left",  "width":80,   "dataIndex":"unitName" },
                        {"header":"单价",align:"left",  "width":80,   "dataIndex":"price" },
                        {"header":"数量",align:"left",  "width":80,   "dataIndex":"amount" },
                        {"header":"金额",align:"right",  "width":80,   "dataIndex":"money" }
                    ]
                    
                }
            }
        ],
        
        layoutConfig : {
            columns : 2,
            width:[0.5,0.5]
       	},
        
		layout : [
		    {
                items:[{'name':"search",'colspan':2}]
            },
            {
                items:[{'name':"gridMain"},{'name':"gridSub"}]
            }
        ],
        
        //-------------------------------------------------------------------------------
        //frame 的构成元素可以响应的事件的描述
        //此示例中，同时演示了查询条件中，多级 下拉列表联动 的实现
        //-------------------------------------------------------------------------------
        bindEvent2Items : function(){   
			//主、从grid
			var grid = $('gridMain');
			var subgrid = $('gridSub');
	
			//查询事件
			$('search').addButtonHandler('查询',function(){
				subgrid.removeAll();
				queryData();
		    });
			
			//重置
			$('search').addButtonHandler('重置',function(){
				setInitValue();
		    });
			
            //新增
            grid.addButtonHandler('新增',function(){
                Sail.open("distributionDetail.html","add",{},MIDDLE_MODWIN_WIDTH,MIDDLE_MODWIN_HEIGHT);
                grid.load();
            });
            
            //复制
            grid.addButtonHandler('复制',function(){
            	var ids = grid.getSelections('id');
            	if (ids.length <= 0){
            		Ext.Msg.alert("提示","复制前，请选择一个单据作为样本！");
            		return;
            	}
            	
            	if (ids.length != 1){
            		Ext.Msg.alert("提示","复制新增时，您只能指定一个单据作为样本！");
            		return;
            	}
            	
               Sail.open("distributionDetail.html","update",{"id":ids[0], "isCopyAdd":"true"},MIDDLE_MODWIN_WIDTH,MIDDLE_MODWIN_HEIGHT);
               grid.load();
            });
         
            //删除
            grid.addButtonHandler('删除',function(){
            	var ids = grid.getSelections('transPid').join(':');
            	if (ids == '')
            	{
            		Ext.Msg.alert("提示","请在列表中选择您要“删除”的单据！");
            		return;
            	}
            	
            	var statuses = grid.getSelections('billStatus').join(':');
            	if( statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_AUDIT)!=-1
            			|| statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_AFFIRM)!=-1
            	  )
            	{
            		
            		Ext.Msg.alert('警告','已“审批”或“确认”的单据不能被删除！');
            		return;
            		
            	}
            	
            	var creatorList = grid.getSelections('creatorId').distinct();
            	if(creatorList.length > 0){
            		var len = creatorList.length;
            		for(var i=0; i<len; i++){
            			var creatorId = creatorList[i];
            			if( loginUserId != creatorId && loginUserId != '1001' ){
            				Ext.Msg.alert("警告","您只能删除自己新增的单据！");
                    		return;
            			}
            		}
            	}
            	
                if(confirm("您确认要”删除“所选择的单据吗？")){
                    var ids = grid.getSelections('transPid').join(':');
                    Sail.postData('../../distribution.do?method=delete',
                                {'f_transIds':ids},
                                function(flag,getData){
                                    if(flag == "true"){
                                        Ext.Msg.alert("提示","操作成功!");
                                        grid.load();
                                        subgrid.load();
                                    }
                                    else{
                                        Ext.Msg.alert("警告",getData.dataset.response.content);
                                    }
                                });
                }
            });
            
            
//            //审批
//            grid.addButtonHandler('审批',function(){
//            	var ids = grid.getSelections('transPid').join(':');
//            	if (ids == '')
//            	{
//            		Ext.Msg.alert("提示","请在列表中选择您要“审批”的单据！");
//            		return;
//            	}
//            	
//            	var statuses = grid.getSelections('billStatus').join(':');
//            	if( statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_AUDIT)!=-1
//            			|| statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_AFFIRM)!=-1
//            	  )
//            	{
//            		
//            		Ext.Msg.alert('警告','已“审批”或“确认”的单据不能被重复“审批”！');
//            		return;
//            	}
//            	
//                if(confirm("您确认要”审批“所选择的单据吗？")){
//                	var result = Sail.open("../../sec/validate_pwd.html","add",{},400,200);
//                	if (!result)
//                	{
//                		return;
//                	}
//                	
//                    var ids = grid.getSelections('transPid').join(':');
//                    Sail.postData('../../../DataMap?funcname=addsimpletrans',
//                                {
//                    				'f_transIds':ids, 
//                    				'f_transTypeId':TRANS_TYPE_AUDIT,
//                    				'f_billStatus':REPAIRS_BILL_STATUS_OF_AUDIT
//                    	         },
//                                function(flag,getData){
//                                    if(flag == "true"){
//                                        Ext.Msg.alert("提示","操作成功!");
//                                        grid.load();
//                                        subgrid.load();
//                                    }
//                                    else{
//                                        Ext.Msg.alert("警告",getData.dataset.response.content);
//                                    }
//                                });
//                }
//            });
//            
//            //取消审批
//            grid.addButtonHandler('取消审批',function(){
//            	var ids = grid.getSelections('transPid').join(':');
//            	if (ids == '')
//            	{
//            		Ext.Msg.alert("提示","请在列表中选择您要“取消审批”的单据！");
//            		return;
//            	}
//            	
//            	var statuses = grid.getSelections('billStatus').join(':');
//            	if( statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_AFFIRM)!=-1
//            			|| statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_EDITING)!=-1
//            	  )
//            	{
//            		
//            		Ext.Msg.alert('警告','已“确认”或未“审批”的单据不能被“取消审批”！');
//            		return;
//            	}
//            	
//                if(confirm("您确认要”取消审批“所选择的单据吗？")){
//                	var result = Sail.open("../../sec/validate_pwd.html","add",{},400,200);
//                	if (!result)
//                	{
//                		return;
//                	}
//                	
//                    var ids = grid.getSelections('transPid').join(':');
//                    Sail.postData('../../../DataMap?funcname=removesimpletrans',
//                                {
//                    				'f_transIds':ids, 
//                    				'f_transTypeId':TRANS_TYPE_AUDIT,
//                    				'f_billStatus':REPAIRS_BILL_STATUS_OF_EDITING
//                    	         },
//                                function(flag,getData){
//                                    if(flag == "true"){
//                                        Ext.Msg.alert("提示","操作成功!");
//                                        grid.load();
//                                        subgrid.load();
//                                    }
//                                    else{
//                                        Ext.Msg.alert("警告",getData.dataset.response.content);
//                                    }
//                                });
//                }
//            });
//            
//           //确认
//            grid.addButtonHandler('确认',function(){
//            	var ids = grid.getSelections('transPid').join(':');
//            	if (ids == '')
//            	{
//            		Ext.Msg.alert("提示","请在列表中选择您要“确认”的单据！");
//            		return;
//            	}
//            	
//            	var statuses = grid.getSelections('billStatus').join(':');
//            	if( statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_AFFIRM)!=-1 )
//            	{
//            		
//            		Ext.Msg.alert('警告','已“确认”的单据不能被重复“确认”！');
//            		return;
//            	}
//            	
//            	if( statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_EDITING)!=-1 )
//            	{
//            		
//            		Ext.Msg.alert('警告','未“审批”的单据不能被“确认”！');
//            		return;
//            	}
//            	
//                if(confirm("您确认要”确认“所选择的单据吗？")){
//                    var ids = grid.getSelections('transPid').join(':');
//                    Sail.postData('../../../DataMap?funcname=addsimpletrans',
//                                {
//                    				'f_transIds':ids, 
//                    				'f_transTypeId':TRANS_TYPE_AFFIRM,
//                    				'f_billStatus':REPAIRS_BILL_STATUS_OF_AFFIRM
//                    	         },
//                                function(flag,getData){
//                                    if(flag == "true"){
//                                        Ext.Msg.alert("提示","操作成功!");
//                                        grid.load();
//                                        subgrid.load();
//                                    }
//                                    else{
//                                        Ext.Msg.alert("警告",getData.dataset.response.content);
//                                    }
//                                });
//                }
//            });
//            
//            
//          //取消确认
//            grid.addButtonHandler('取消确认',function(){
//            	var ids = grid.getSelections('transPid').join(':');
//            	if (ids == '')
//            	{
//            		Ext.Msg.alert("提示","请在列表中选择您要“取消确认”的单据！");
//            		return;
//            	}
//            	
//            	var statuses = grid.getSelections('billStatus').join(':');
//            	if( statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_AUDIT)!=-1
//            			|| statuses.lastIndexOf(REPAIRS_BILL_STATUS_OF_EDITING)!=-1
//            	  )
//            	{
//            		
//            		Ext.Msg.alert('警告','只能对已“确认”的单据进行“取消确认”！');
//            		return;
//            	}
//            	
//                if(confirm("您确认要”取消确认“所选择的单据吗？")){
//                    var ids = grid.getSelections('transPid').join(':');
//                    Sail.postData('../../../DataMap?funcname=removesimpletrans',
//                                {
//                    				'f_transIds':ids, 
//                    				'f_transTypeId':TRANS_TYPE_AFFIRM,
//                    				'f_billStatus':REPAIRS_BILL_STATUS_OF_AUDIT
//                    	         },
//                                function(flag,getData){
//                                    if(flag == "true"){
//                                        Ext.Msg.alert("提示","操作成功!");
//                                        grid.load();
//                                        subgrid.load();
//                                    }
//                                    else{
//                                        Ext.Msg.alert("警告",getData.dataset.response.content);
//                                    }
//                                });
//                }
//            });
            
            //修改
            grid.addAction('修改',function(id){
                Sail.open("distributionDetail.html","update",{"id":id},MIDDLE_MODWIN_WIDTH,MIDDLE_MODWIN_HEIGHT);
                grid.load();
            });
            
            //选择首行,显示子列表数据
            grid.on('afterload',function(){
                grid.selectFirstRow();
            });
            
            //主列表行事件,显示子列表数据
            grid.on('rowclick',function(id,row,record){
            	//$('gridSub').load("../../../DataMap?funcname=fetchdetail&f_transId="+id);
            	if (id == "") {
        			return;
        		}
        		var transactionId=record.get("transactionId");
        		if (transactionId == "") {
        			return;
        		}
          	var url = "../../distribution.do?method=listDetail&transactionId="+transactionId;
          	$('gridSub').load(url);
            }); 
        }        
    },
    //进入页面自动查询数据
	afterRun : function(){
    	//设初值
    	setInitValue();
//    	if (!$('gridMain').getButton("审批").disabled && loginUserId!="1001")
//    	{
//    		$('配送部门').setData(this.getOperorgid(), this.getOperorgname());
//    	}
    	//查询数据
    	queryData();
	},
	
	afterLoadLoginInfo:function(){
		loginUserId = this.getUser();
		loginUserName = this.getOpername();
	}
});

page1.run();

function setInitValue()
{
	//单据日期初始化
	var beginDate = getMonthFirstDay();
	$('单据日期从').setValue(beginDate);
	var endDate=getCurrentDay();
	$('至').setValue(endDate);
	if (!$('gridMain').getButton("新增").disabled)
	{	
		$('录入人').setValue(loginUserId);
		$('录入人').setText(loginUserName);
	}
}
function queryData(){
	var searchInfo = $('search').getSearch();
	var url = "../../distribution.do?method=list&billTypeId="+BILL_TYPE_DISTRIBUTION+searchInfo;
	$('gridMain').load(url);
}

